<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Cookie,Log,Exception,Auth;
use App\Services\CommonService;
use App\Services\WechatOpenidService;
use App\Models\Settingauthtxt;
use App\Models\Users;

class HomeController extends Controller
{
    public function webIndex(Request $request)
    {
        return redirect("/app");
    }

    public function appIndex(Request $request)
    {
        $isWeChat = strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
        if ($isWeChat) {
            if (!Auth::check()) {
                $openInfo = WechatOpenidService::getOpenid();
                if ($openInfo["type"]=="jump") {
                    return redirect()->away($openInfo["url"]);
                } elseif($openInfo["type"]=="error") {
                    return view("links.notice",["msg"=>"您暂未配置微信公众号!"]);
                } else {
                    if (empty($openInfo["openid"])) return view("links.notice",["msg"=>"公众号信息填写错误!"]);
                    $users = Users::getUsersByOpenid($openInfo["openid"]["openid"]);
                    $userInfo = WechatOpenidService::officialUsersInfo($openInfo["openid"]["access_token"],$openInfo["openid"]["openid"]);
                    $headImg = $userInfo["headimgurl"];
                    $nickName = $userInfo["nickname"];
                    $bindPhone = false;
                    if (empty($users)) {
                        $users_id = Users::creatByOpenId($openInfo["openid"]["openid"],$headImg,$nickName);
                        $bindPhone = true;
                    } else {
                        $users_id = $users->id;
                        Users::updateByOpenId($openInfo["openid"]["openid"],$headImg,$nickName);
                        if (empty($users->username)) {
                            $bindPhone = true;
                        }
                    }
                    Auth::loginUsingId($users->id,true);
                    if ($bindPhone) {
                        return redirect("/admin/users/users_phone");
                    }
                }
            } else {
                $users = Auth::user();
                if (empty($users->username)) {
                    return redirect("/admin/users/users_phone");
                }
            }
        }
        if (!empty($request->users_id)) {
            $users_id = decrypt($request->users_id);
            Auth::loginUsingId($users_id,true);
        }
        return view('admin.index.app');
    }

    public function getLoginUrl(Request $request)
    {
        $users_id = Users::creatUsersByOpenid("","",$request->name,"");
        $url = "https://bab.moreqifu.cn/app?users_id=".encrypt($users_id);
        echo $url;
    }

    public function usersCenter(Request $request)
    {
        return view('admin.index.user_center');
    }

    public function settingAuthTxt(Request $request,$name)
    {   
        // 后台认证文件
        $txt = Settingauthtxt::getSettingAuthTxt($name);
        return empty($txt) ? "" : $txt->txt;
    }
}
